﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using syc.Helper;
using System.IO;
namespace syc.Jidong
{
    public partial class 能源消耗月报表 : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            this.GridView1.Visible = true;

            if (this.RiQiStart.Text == "" || this.RiQiEnd.Text == "")
            {
                return;
            }
            else
            {
                DateTime date1 = DateTime.Parse(this.RiQiStart.Text);
                DateTime date2 = DateTime.Parse(this.RiQiEnd.Text);
                this.SqlDataSource1.SelectCommand = @"select * from 能源统计表 where 日期 >='" + date1 + "' and 日期 <='" + date2 + "'";
                this.ViewState["SelectCommand"] = this.SqlDataSource1.SelectCommand;

                GridViewHelper helper = new GridViewHelper(this.GridView1);
                helper.RegisterGroup("部门名称", true, true);
                helper.RegisterSummary("电量", SummaryOperation.Sum, "部门名称");
                helper.RegisterSummary("电价", SummaryOperation.Sum, "部门名称");
                helper.RegisterSummary("自来水", SummaryOperation.Sum, "部门名称");
                helper.RegisterSummary("水价", SummaryOperation.Sum, "部门名称");
                helper.GroupSummary += new GroupEvent(helper_GroupSummary);
                helper.RegisterSummary("电量", SummaryOperation.Sum);
                helper.RegisterSummary("电价", SummaryOperation.Sum);
                helper.RegisterSummary("自来水", SummaryOperation.Sum);
                helper.RegisterSummary("水价", SummaryOperation.Sum);
                helper.ApplyGroupSort();
            }     

        }
        private void helper_GroupSummary(string groupName, object[] values, GridViewRow row)
        {
            row.Cells[0].HorizontalAlign = HorizontalAlign.Right;
            row.Cells[0].Text = "小计";             
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //ToExcel(this.Page, "application/ms-xls ", "nengYuan");
        }
       
        public void ToExcel(System.Web.UI.Control ctl, String filetype, String filename)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + Server.UrlEncode(filename) + ".xls");

            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            //更改ContentType的值为ms-word即可实现导出到Word  
            HttpContext.Current.Response.ContentType = filetype;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword  
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {

        }
    }
}
